package com.dd.ent.car.ui.home;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.NewsList;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.common.interf.OnTabReselectListener;
import com.dd.ent.car.ui.adapter.ViewPageFragmentAdapter;
import com.dd.ent.car.ui.home.templet.Base.TempletBaseFragment;
import com.dd.ent.car.ui.note.NoteChoiceFragment;
import com.dd.ent.car.ui.home.base.BaseViewPagerFragment;
import com.dd.ent.car.ui.home.base.HomeBaseListFragment;
import com.dd.ent.car.ui.home.templet.HomeTempletListFragment;
import com.dd.ent.car.ui.home.templet.choice.ChoiceFragment;
import com.dd.ent.car.ui.home.templet.video.VideoFragment;
import com.dd.ent.car.ui.widget.PagerSlidingTabStrip;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;

/**
 * 综合Tab界面
 */
public class HomeFragment extends BaseViewPagerFragment implements
        OnTabReselectListener, PagerSlidingTabStrip.OnPagerChangeLis {

    private final static String TAG = "HomeFragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MLog.debug(TAG,"onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MLog.debug(TAG,"onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabStrip.setOnPagerChange(this);
        MLog.debug(TAG, "onViewCreated");
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onSetupTabAdapter(ViewPageFragmentAdapter adapter) {
        String[] title = getResources().getStringArray(
                R.array.general_viewpage_arrays);

        adapter.addTab(title[0], "choice", ChoiceFragment.class,
                getBundle(NewsList.CATALOG_ALL));
        adapter.addTab(title[1], "video", VideoFragment.class,
                getBundle(NewsList.CATALOG_WEEK));
        adapter.addTab(title[2], "forum", NoteChoiceFragment.class,
                getBundle(NewsList.CATALOG_LATEST));
//        adapter.addTab(title[2], "piece", GraphicFragment.class,
//                getBundle(BlogList.CATALOG_LATEST));
    }

    private Bundle getBundle(int newType) {
        Bundle bundle = new Bundle();
        bundle.putInt(HomeBaseListFragment.BUNDLE_KEY_CATALOG, newType);
        return bundle;
    }

    @Override
    protected void setScreenPageLimit() {
        mViewPager.setOffscreenPageLimit(3);
    }

    /**
     * 基类会根据不同的catalog展示相应的数据
     *
     * @param catalog 要显示的数据类别
     * @return
     */
    private Bundle getBundle(String catalog) {
        Bundle bundle = new Bundle();
        bundle.putString(VideoFragment.BUNDLE_BLOG_TYPE, catalog);
        return bundle;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void initView(View view) {
    }

    @Override
    public void initData() {

    }

    @Override
    public void onTabReselect() {
        Fragment fragment = mTabsAdapter.getItem(mViewPager.getCurrentItem());
        if (fragment != null && fragment instanceof HomeTempletListFragment) {
            ((HomeTempletListFragment) fragment).onTabReselect();
        }
    }

    @Override
    public void onChanged(int page) {
        MLog.debug(TAG, "onChanged page=" + page);
        for (Fragment fragment : mTabsAdapter.getAllFragment()){
            if (fragment != null && fragment instanceof TempletBaseFragment) {
                ((TempletBaseFragment) fragment).onPageChanged(page);
            }
        }
    }


    @UIHandler(UIConfig.CHANGE_TITLE_BACKGROUND)
    public void onChangeTitleBackgroundRes(int resId,boolean isUp){
        mTabStrip.setBackgroundResource(resId);
        if (isUp){
            viewPagerLine.setVisibility(View.GONE);
        }else{
            viewPagerLine.setVisibility(View.VISIBLE);
        }
    }


}